package neusoft.edu.cn.guanshangshiguang.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import neusoft.edu.cn.guanshangshiguang.Listener.ListListener;
import neusoft.edu.cn.guanshangshiguang.Listener.OnItemClickListener;
import neusoft.edu.cn.guanshangshiguang.R;
import neusoft.edu.cn.guanshangshiguang.activities.ArticleDetailsActivity;
import neusoft.edu.cn.guanshangshiguang.adapter.PersonalBookListAdapter;
import neusoft.edu.cn.guanshangshiguang.bean.PersonalBookListBean;
import neusoft.edu.cn.guanshangshiguang.model.PersonalBookListModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class ReadFragmentPersonal extends Fragment {

    private List<PersonalBookListBean> List;
    RecyclerView recyclerView;
    PersonalBookListAdapter viewAdapter;
    SwipeRefreshLayout swipeRefreshLayout;
    private int flag=0;

    ListListener listListener= new ListListener() {
        @Override
        public void onResponse(List list) {
            List = list;
            viewAdapter.setDate(List);
            if(flag==1){
                flag=0;
                swipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getContext(), "失败："+msg, Toast.LENGTH_SHORT).show();
        }
    };


    OnItemClickListener onItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            Intent intent = new Intent(getContext(),ArticleDetailsActivity.class);
            intent.putExtra("bid",List.get(position).getBid());
            startActivity(intent);
        }
    };

    public ReadFragmentPersonal() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_read_fragment_creation, container, false);
    }

    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerViewInit(view);

        PersonalBookListModel ListModel = new PersonalBookListModel();
        ListModel.getResultList(listListener);

    }
    private void RecyclerViewInit(View view) {
        viewAdapter = new PersonalBookListAdapter();

        recyclerView = view.findViewById(R.id.recyclerView3);

        //布局管理器  可以设置显示数据的方式为垂直或者水平
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);

        swipeRefreshLayout = view.findViewById(R.id.swipeRefresh);

        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorAccent),
                getResources().getColor(R.color.colorPrimaryDark));

        swipeRefreshLayout.setProgressViewOffset(true,50,60);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                flag=1;
                PersonalBookListModel ListModel = new PersonalBookListModel();
                ListModel.getResultList(listListener);
            }
        });

        recyclerView.setLayoutManager(linearLayoutManager);
        viewAdapter.setOnItemClickListener(onItemClickListener);
        recyclerView.setAdapter(viewAdapter);
    }

}